﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using JohnnySins.DAL;
using JohnnySins.DAL.Repositories;
using JohnnySins.Entities;
using JohnnySins.Code;
using System.IO;
using System.Configuration;
using System.Net.Mail;

namespace JohnnySins
{
    public partial class Audition : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ClientScript.RegisterOnSubmitStatement(this.GetType(), "OnSubmitScript", string.Format("if (Page_ClientValidate('newUser')) {{ $('#{0}').hide(); $('#{1}').show(); }}", btnSend.ClientID, btnSendFake.ClientID));
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                StarProfile itm = new StarProfile();

                itm.FirstName = txtFirstName.Text;
                itm.LastName = "";
                itm.Email = txtEmail.Text;
                itm.Phone = txtPhone.Text;
                itm.Gender = "Female";
                itm.AuditionMessage = txtAuditionMessage.Text;
                itm.ContactPurposeId = Convert.ToInt32(ddlContactPurposeId.SelectedValue);
                /*
                 itm.Website = txtWebsite.Text;
                 itm.State = txtState.Text;*/
                 if (chkMemberId.Checked)
                    itm.MemberId = txtMemberId.Text;
                repo.InsertAudition(itm);
                SavePhoto(fuFile1, itm, "Contact Photo 1");
                
                string toEmail = ConfigurationManager.AppSettings["AuditionEmail"];
                string messageHtml = File.ReadAllText(Server.MapPath("~/Files/Templates/Audition.html"));
                messageHtml = messageHtml.Replace("%name%", itm.FirstName).Replace("%email%", itm.Email).Replace("%phone%", itm.Phone).
                    Replace("%purpose%", ddlContactPurposeId.SelectedItem.Text).Replace("%memberid%", itm.MemberId).Replace("%message%", itm.AuditionMessage);
                
                List<Attachment> att = new List<Attachment>();
                if (itm.Photos != null)
                {
                    string profilePath = Server.MapPath(string.Format("~/Files/StarProfile/{0}/", itm.StarProfileId.ToString()));
                    profilePath = Path.Combine(profilePath, "Photo");
                    string filePath = Path.Combine(profilePath, itm.Photos[0].PhysFileName);
                    att.Add(new Attachment(filePath, itm.Photos[0].MimeType));
                }
                EmailSender.Send(toEmail, "New audition", messageHtml, true, att);

                if (chkSendCopy.Checked)
                    EmailSender.Send(txtEmail.Text, "New audition", messageHtml, true, att);

                ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Alert", string.Format("alert('{0}');", "You registered successfully!"), true);

                txtFirstName.Text = "";
                txtEmail.Text = "";
                txtPhone.Text = "";
                txtAuditionMessage.Text = "";
                txtMemberId.Text = "";
            }
        }

        private void SavePhoto(FileUpload fu, StarProfile itm, string title)
        {
            if (!fu.HasFile)
                return;

            string[] extImg = new string[] { ".jpeg", ".jpg", ".png", ".gif", ".bmp" };
            string extFile = Path.GetExtension(fu.FileName);
            if (!extImg.Contains(extFile))
                return;

            string profilePath = Server.MapPath(string.Format("~/Files/StarProfile/{0}/", itm.StarProfileId.ToString()));
            profilePath = Path.Combine(profilePath, "Photo");
            if (!Directory.Exists(profilePath))
                Directory.CreateDirectory(profilePath);
            using (var repo = RepositoryFactory.ResolveRepository<StarProfileRepository>())
            {
                StarProfilePhoto itmPhoto = new StarProfilePhoto();
                itmPhoto.StarProfileId = itm.StarProfileId;
                itmPhoto.StarProfilePhotoTitle = title;
                itmPhoto.IsActive = true;
                itmPhoto.StarProfilePhotoFileName = fu.FileName;
                repo.InsertPhoto(itmPhoto);

                string filePath = Path.Combine(profilePath, itmPhoto.PhysFileName);
                fu.SaveAs(filePath);

                itm.Photos = new List<StarProfilePhoto>();
                itm.Photos.Add(itmPhoto);
            }
        }

        protected void chbMemberId_CheckedChanged(object sender, EventArgs e)
        {
            pnlMemberId.Visible = reqMemberId.Enabled = chkMemberId.Checked;
        }
    }
}